package com.yycx.module.bus.provider.handler.finance;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusFinance;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.provider.service.BusFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 部门对账上期列表
 *
 * @author zyf
 */
@Component("finacePageListHandler")
public class FinacePageListHandler implements PageInterceptor<BusOrderDetails> {


    @Autowired
    private BusFinanceService busFinanceService;

    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        Long financeId = params.getLong("financeId");
        BusFinance benQiHjFinance = busFinanceService.getById(financeId);
        //本期月份
        String financeMonth = benQiHjFinance.getFinanceMonth();
        //上期日期
        LocalDate lastMonthDate = LocalDate.parse(financeMonth + "-01");
        Long saleManId = benQiHjFinance.getSaleManId();
        Long deptId = benQiHjFinance.getDeptId();
        cq.addSelect(BusDept.class, "deptName");
        cq.addSelect(BusSaleman.class, "saleManName");
        cq.lt("bod.saleDate", lastMonthDate);
        cq.eq(true, "bod.saleManId", saleManId);
        cq.eq(true, "bod.deptId", deptId);
        cq.createJoin(BusDept.class);
        cq.createJoin(BusSaleman.class);
        cq.orderByDesc("saleDate");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusOrderDetails> cq, List<EntityMap> result, EntityMap extra) {
        Long financeId = cq.getLong("financeId");
        BusFinance benQiHjFinance = busFinanceService.getById(financeId);
        initLastBusFinanceDetails(result, benQiHjFinance);
        return result;
    }


    private void initLastBusFinanceDetails(List<EntityMap> orderDetailsList, BusFinance benQiHjFinance) {
        //当期第一天
        String lastFirstDay = benQiHjFinance.getFinanceMonth() + "-01";
        //本期月份最后一天
        String lastDay = DateUtils.format(DateUtils.getLastDayOfMonth(lastFirstDay));
        if (FlymeUtils.isNotEmpty(orderDetailsList)) {
            for (EntityMap orderDetails : orderDetailsList) {
                String renkuanDate = orderDetails.get("renkuanDate");
                //排除当期之后回款数据
                if (FlymeUtils.isNotEmpty(renkuanDate)) {
                    if (DateUtils.gt(renkuanDate + "-01", lastDay)) {
                        orderDetails.put("returnDate", "");
                        orderDetails.put("returnAmount", 0);
                    }
                }
                BigDecimal customBond = orderDetails.getBigDecimal("customBond");
                BigDecimal overAmount = orderDetails.getBigDecimal("customOverBond");
                BigDecimal customBondAmount = customBond.add(overAmount);
                orderDetails.put("customBondAmount", customBondAmount);


                //应结算金额
                BigDecimal settleAmount = orderDetails.getBigDecimal("settleAmount");
                BigDecimal settleAmount2 = orderDetails.getBigDecimal("settleAmount2");

                //已结算金额
                BigDecimal settleRealAmount = orderDetails.getBigDecimal("settleRealAmount");
                BigDecimal settleRealAmount2 = orderDetails.getBigDecimal("settleRealAmount2");
                BigDecimal noSettleAmount = new BigDecimal("0");

                //结算日期
                String settlePayDate = orderDetails.get("settlePayDate");
                String settlePayDate2 = orderDetails.get("settlePayDate2");

                //应结算总金额
                BigDecimal settleTotalAmount = settleAmount.add(settleAmount2);
                //应结算总金额
                orderDetails.put("settleAmount", settleTotalAmount);
                BigDecimal yesSettleAmount = new BigDecimal("0");

                if (FlymeUtils.isNotEmpty(settlePayDate)) {
                    if (DateUtils.gt(settlePayDate, lastDay)) {
                        orderDetails.put("settleRealAmount", 0);
                        orderDetails.put("settlePayDate", "");
                    } else {
                        yesSettleAmount = yesSettleAmount.add(settleRealAmount);
                    }
                }

                if (FlymeUtils.isNotEmpty(settlePayDate2)) {
                    if (DateUtils.gt(settlePayDate2, lastDay)) {
                        orderDetails.put("settleRealAmount2", new BigDecimal("0"));
                        orderDetails.put("settlePayDate2", "");
                    } else {
                        yesSettleAmount = yesSettleAmount.add(settleRealAmount2);
                    }

                }
                noSettleAmount = settleTotalAmount.subtract(yesSettleAmount);
                orderDetails.put("yesSettleAmount", yesSettleAmount);
                orderDetails.put("noSettleAmount", noSettleAmount);

                String customBondRetDate = orderDetails.get("customBondRetDate");

                if (FlymeUtils.isNotEmpty(customBondRetDate)) {
                    if (DateUtils.gt(customBondRetDate, lastDay)) {
                        orderDetails.put("returnCustomBond", 0);
                        orderDetails.put("returnCustomBondDate", "");
                    } else {
                        orderDetails.put("returnCustomBondDate", customBondRetDate);
                    }
                }

            }
        }
    }
}
